cmake_minimum_required(VERSION 3.15)
project(drc_system_pybind VERSION 1.0.0 LANGUAGES CXX)

# 设置C++标准
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# 设置构建类型
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()

# 设置编译选项
if(MSVC)
    add_compile_options(/W4)
else()
    add_compile_options(-Wall -Wextra -Wpedantic)
endif()

# 查找Python
find_package(Python 3.8 COMPONENTS Interpreter Development REQUIRED)

# 添加本地pybind11
add_subdirectory(pybind11)

# 添加头文件目录
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${CMAKE_CURRENT_SOURCE_DIR}/pybind11/include
    ${Python_INCLUDE_DIRS}
)

# 添加源文件和头文件
set(DRC_CORE_HEADERS
    include/drc_core.h
    include/schematic_types.h
)

set(DRC_CORE_SOURCES
    src/drc_core.cpp
    ${DRC_CORE_HEADERS}
)

# 创建核心库
add_library(drc_core STATIC ${DRC_CORE_SOURCES})
target_link_libraries(drc_core PRIVATE pybind11::embed)

# 创建Python模块
pybind11_add_module(drc_core_python src/pybind_wrapper.cpp)
target_link_libraries(drc_core_python PRIVATE drc_core)

# 添加示例程序
add_executable(run_drc examples/run_drc.cpp)
target_link_libraries(run_drc PRIVATE drc_core ${Python_LIBRARIES})

# 复制规则文件到构建目录
file(COPY rules DESTINATION ${CMAKE_BINARY_DIR})

# 安装配置
install(TARGETS drc_core drc_core_python run_drc
    RUNTIME DESTINATION bin
    LIBRARY DESTINATION lib
    ARCHIVE DESTINATION lib
)
install(FILES ${DRC_CORE_HEADERS} DESTINATION include)
install(DIRECTORY rules/ DESTINATION rules)

# 添加测试支持
enable_testing()
add_subdirectory(tests)

# 输出配置信息
message(STATUS "Python version: ${Python_VERSION}")
message(STATUS "Python include dirs: ${Python_INCLUDE_DIRS}")
message(STATUS "Python libraries: ${Python_LIBRARIES}")
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")

# 设置输出目录
set_target_properties(drc_core_python PROPERTIES
    LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
set_target_properties(run_drc PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
) 